package com.qibo.baselib.base;

import android.app.Application;
import android.content.Context;
import android.support.multidex.MultiDex;
import android.widget.Toast;

import com.hjq.toast.ToastUtils;
import com.qibo.baselib.api.BaseAppConfig;
import com.qibo.baselib.utils.TypefaceUtil;
import com.tencent.bugly.crashreport.CrashReport;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;

public abstract class BaseApplication extends Application {
    public static BaseApplication instance;
    public static BaseApplication getInstance() {
        return instance;
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
        instance = this;
    }

    @Override
    public void onCreate() {
        init(this);
        super.onCreate();
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
//                .addInterceptor(new LoggerInterceptor("TAG"))
                .connectTimeout(10000L, TimeUnit.MILLISECONDS)
                .readTimeout(10000L, TimeUnit.MILLISECONDS)
                //其他配置
                .build();
        OkHttpUtils.initClient(okHttpClient);

        CrashReport.initCrashReport(getApplicationContext(), "0c833ba4a2", true);
        if(BaseAppConfig.iSopenException){
            install();
        }
        TypefaceUtil.replaceSystemDefaultFont(this,"fonts/pingfang-sc-regular.ttf");
        ToastUtils.init(this);
    }

    public abstract void init(Context context);

    private void install() {
        final Thread.UncaughtExceptionHandler sysExcepHandler = Thread.getDefaultUncaughtExceptionHandler();
        final Toast toast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
//        Cockroach.install(new ExceptionHandler() {
//            @Override
//            protected void onUncaughtExceptionHappened(Thread thread, Throwable throwable) {
//                Log.e("AndroidRuntime", "--->onUncaughtExceptionHappened:" + thread + "<---", throwable);
//                //CrashLog.saveCrashLog(getApplicationContext(), throwable);
//                new android.os.Handler(Looper.getMainLooper()).post(new Runnable() {
//                    @Override
//                    public void run() {
////                        toast.setText(R.string.safe_mode_excep_tips);
////                        toast.show();
//                    }
//                });
//            }
//
//            @Override
//            protected void onBandageExceptionHappened(Throwable throwable) {
//                throwable.printStackTrace();//打印警告级别log，该throwable可能是最开始的bug导致的，无需关心
////                toast.setText("Cockroach Worked");
////                toast.show();
//            }
//
//            @Override
//            protected void onEnterSafeMode() {
////                int tips = R.string.safe_mode_tips;
////                Toast.makeText(App.this, getResources().getString(tips), Toast.LENGTH_LONG).show();
////                DebugSafeModeUI.showSafeModeUI();
//
////                if (BuildConfig.DEBUG) {
////                    Intent intent = new Intent(this, DebugSafeModeTipActivity.class);
////                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
////                    startActivity(intent);
////                }
//            }
//
//            @Override
//            protected void onMayBeBlackScreen(Throwable e) {
//                Thread thread = Looper.getMainLooper().getThread();
//                Log.e("AndroidRuntime", "--->onUncaughtExceptionHappened:" + thread + "<---", e);
//                //黑屏时建议直接杀死app
//                sysExcepHandler.uncaughtException(thread, new RuntimeException("black screen"));
//            }
//
//        });

    }
}